<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\GuildOp;
use App\Http\Controllers\Controller;
use App\Model\Guild\Guild;
use App\Model\Guild\GuildHost;
use App\Model\Message;
use App\Model\UserProfile;
use App\Util\InfoUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use Illuminate\Database\Eloquent\Model;

/**
 * 内部测试工具
 */
class SlientPushController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        // return $this->create();
        return Admin::content(function (Content $content) {
            $content->header('透传测试');
            $content->description('');
            $content->body($this->grid());
        });
    }


    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('透传测试');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a form builder.
     * @return Form
     */
    protected function form($mode = '', $id = '')
    {
        return Admin::form(UserProfile::class, function (Form $form) {
            $form->text('user_profile_id', '用户ID');
            $form->select('msg_type', '透传消息')->options(Message::SILENT_MSG_TYPE);
            $extInfo = [
                'tip1' => '今日充值即可获得以下特权',
                'tip2' => '今日无限发送私信，获得3分钟免费音视频通话优惠券',
                'btn_text' => '充值任意金额 马上领礼包'
            ];
            $form->text('extra_info', '额外信息')->default(json_encode($extInfo));

            $form->saving(function (Form $form) {
                $profileId = $form->user_profile_id;
                $msgType = $form->msg_type;
                $userId = UserProfile::getUidByProfileId($profileId);
                if (!$userId) return InfoUtil::adminErr('用户信息不存在！');
                $extInfo = json_decode($form->extra_info, true);
                $ret = Message::sendSlient($userId, $msgType, $extInfo);
                return InfoUtil::adminSuc('推送结果：' . $ret);
            });

            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }

    function guild_op()
    {
        $id = request('id');
        $status = intval(request('status'));
        $record = Guild::find($id);
        if (!$record) return ['ret' => -1, 'message' => '该代理记录不存在'];
        $record->update(['status' => $status]);
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
